dojo.provide("dojo.app.myvalidator.validatormain");

dojo.declare("dojo.app.myvalidator.validatormain", null, {
    _element : null,
    _idError : "",
    _options : {},
    _trigerredEvents : [],
    _showErrorFunction : null,
    _validatorsStack : [],
    _countProcessors : 0,

    constructor: function(validationElement) {
        if (validationElement.element != "undefined") {
            this._element = validationElement.element;
        };
        if (validationElement.options != "undefined") {
            this._options = validationElement.options;
        };
        this._validatorsStack = [];
        this._idError = "error" + this._element.attr("id");
    },

    initOptions: function(globalOptions) {
        this._options = dojo.mixin({}, globalOptions, this._options);
        this._trigerredEvents = this._options.triggeredEvents;
        this._countProcessors = this._options.processors.length;
        if (this._options.bindedElement == null) {
            this._options.bindedElement = this._element;
        };
    },

    initValidatorsStack: function(validatorsStack) {
        for (var i=0; i<this._countProcessors; i++) {
            var validationMethod = this._options.processors[i].method;
            var validator = validatorsStack[validationMethod];
            if (typeof(validator) != "undefined") {
                this._validatorsStack[validationMethod] = validator;
            } else {
                console.log("ERROR! Validator " + validationMethod + " has not be setted!");
            };
        };
    },

    initErrorMessages: function(locale) {
        for (var i=0; i<this._countProcessors; i++) {
            var messagesCur = this._options.processors[i].messages;
            var validationMethod = this._options.processors[i].method;
            var validator = this._validatorsStack[validationMethod];
            if (typeof(validator) != "undefined") {
                this._options.processors[i].message = validator.getErrorMessage(locale, messagesCur);
            } else {
                console.log("ERROR! Validator " + validationMethod + " has not be setted!");
            };
        };
    },

    initErrorFunction: function(errorShowFunctionsStack) {
        //Init showErrorFunction
        var errorFunc = errorShowFunctionsStack[this._options.errorFunction];
        if (typeof(errorFunc) != "undefined") {
            this._showErrorFunction = errorFunc;
        };
    },

    validateElement: function() {
        if (this._options.show && this._countProcessors) {
            var counterEvents = this._trigerredEvents.length;
            for(var k=0; k<counterEvents; k++) {
                var event = this._trigerredEvents[k];

                //Use _self as reference to this object
                //and this as bindedElement
                var _self = this;
                //console.log(_self);
                this._options.bindedElement.connect(event, function(e) {
                    //console.log(_self);
                    dojo.query("#" + _self._idError).orphan();
                    //Full stack of messages that will be displayed
                    var messageText = [];
                    var j = 0;

                    for (var i=0; i<_self._countProcessors; i++) {
                        var elemValue = _self._element.attr("value");
                        var neededValue = _self._options.processors[i].validationValue;
                        var processor = _self._options.processors[i];

                        _self._validatorsStack[processor.method].setElementValue(elemValue);
                        _self._validatorsStack[processor.method].setElement(_self._element);

                        var isError = _self._validatorsStack[processor.method].validate(neededValue);

                        if (isError) {
                            var readyText = processor.message.replace(/%value%/g, _self._element.attr("value"));
                            if (typeof(neededValue) != "undefined") {
                                readyText = readyText.replace(/%neededValue%/g, neededValue.toString());
                            };
                            messageText[j++] = readyText;
                        };
                    };
                    if (messageText.length) {
                        //Trigger show error function
                        _self._showErrorFunction.constructor(_self._element, _self._idError, messageText);
                        _self._showErrorFunction.displayErrors();
                        dojo.stopEvent(e);
                        return false;
                    };
                });
            };
        };

    }
});


